package cn.xo68.boot.auth.server.service;

import cn.xo68.boot.auth.core.common.AuthConstants;
import cn.xo68.boot.auth.server.domain.OauthRefreshToken;
import cn.xo68.boot.auth.server.mapper.OauthRefreshTokenMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;



/** 
 * OauthRefreshToken Service
 * @author wuyy(16349023@qq.com)
 * @date 2018年08月26日 20:44:38
 *
 */
@CacheConfig(cacheNames = {AuthConstants.CACHE_NAME_OAUTH2_REFRESH_TOKEN})
 @Service
public class OauthRefreshTokenService{
    @Autowired
    private OauthRefreshTokenMapper oauthRefreshTokenMapper;

    @Cacheable(key = "#refreshToken",unless = "#result == null")
    public OauthRefreshToken get(String refreshToken) {
        return  oauthRefreshTokenMapper.get(refreshToken);
    }

    @CachePut(key = "#oauthRefreshToken.refreshToken")
    public OauthRefreshToken insert(OauthRefreshToken oauthRefreshToken) {
        oauthRefreshTokenMapper.insert(oauthRefreshToken);
        return oauthRefreshToken;
    }
    @CachePut(key = "#oauthRefreshToken.refreshToken")
    public OauthRefreshToken update(OauthRefreshToken oauthRefreshToken) {
        oauthRefreshTokenMapper.update(oauthRefreshToken);
        return oauthRefreshToken;
    }
    @CacheEvict(key = "#refreshToken")
    public void delete(String refreshToken) {
        oauthRefreshTokenMapper.delete(refreshToken);
    }
    
    public List<OauthRefreshToken> list() {
        return oauthRefreshTokenMapper.list();
    }
	
	public PageInfo<OauthRefreshToken> list(int pageNumber,int pageSize) {
        PageHelper.startPage(pageNumber, pageSize,true);
        List<OauthRefreshToken> docs = oauthRefreshTokenMapper.list();
        PageInfo<OauthRefreshToken> pageInfo = new PageInfo<>(docs);
        return pageInfo;
    }
}